%{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *output, *style;

char *title;
char *label;
char *button;
char *color;
char *action;
char *imgPath;

char buttons[5][256];
char actions[5][256];
int buttonIndex;

int positionX;
int positionY;
char position[256];

int yylex();
int yyerror(char *s);
int create();
int printButtons();
int stripQuotes(const char *input, char *output);
int resetVariables();
int printFile(char *path);
int printString(char *string);
int printPosition();
int printColor();
int printImage();
int printError();

%}

%union {

	char *string;
	int integer;

}

%token	<string> QUOTES
%token	<string> LB
%token	<string> RB
%token	<string> BUTTON
%token	<string> C
%token	<string> WINDOW
%token	<string> STRING
%token	<string> END
%token	<string> LABEL
%token	<string> COMMA
%token	<string> POSITION
%token	<string> COLOR
%token	<string> IMAGE
%token	<integer> NUM

%type	<string> Expression
%type	<string> Body
%type	<string> Title
%type	<string> Element

%start	Input

%%

Input:
	  /* Empty */
	| Input Line
	;

Line:
	  END
	| Expression END		{	create(); }
	;

Expression:
	  WINDOW
	| WINDOW Title
	| WINDOW Body
	| WINDOW Title Body
	;

Title:
	  STRING			{	title = $1; }
	;

Body:
	  LB Element
	;

Element:
	  RB												
	| STRING Element		{	label = $1; }
	| LABEL C STRING Element	{	label = $3; }
	| BUTTON C STRING Element	{	button = $3; 
						stripQuotes(button,button); 
						strcpy(buttons[buttonIndex], button); 
						buttonIndex++; }
	| BUTTON C STRING COMMA STRING Element	{	button = $3; 
						stripQuotes(button,button); 
						strcpy(buttons[buttonIndex], button); 
						action = $5; 
						stripQuotes(action,action); 
						strcpy(actions[buttonIndex], action); 
						buttonIndex++;}
	| IMAGE C STRING Element	{	imgPath = $3; }
	| POSITION C NUM COMMA NUM
	  Element			{	positionX = $3; positionY = $5; }
	| COLOR C STRING Element	{	color = $3; }
	;

%%

int main(void) {

	resetVariables();

	yyparse();

}

int yyerror(char *s) {

	printf("%s\n",s);

}

int create() {

	output = fopen("output", "w");
  
	stripQuotes(title,title);
	stripQuotes(label,label);
	stripQuotes(color,color);

	printError();

	printFile("resources/1.text");
	printString("\t$(function() { $( '#dialog' ).dialog({ \n");
	printButtons(output);
	printPosition();
	printString("\t}) }); \n");
	printFile("resources/2.text");
	printString("<div id='dialog' title='");
	printString(title);
	printString("'><p>");
	printImage();
	printString(label);
	printString("</p></div> \n");
	printFile("resources/3.text");

	fclose(output);

	output = fopen("style.css", "w");

	printFile("resources/c1.text");
	printColor();
	printString("}");

	fclose(output);

	resetVariables();

	printf("Compiled OK \n");
 
	return 0;

}

int printButtons() {

	printString("\t\tbuttons: { \n");

	for (int i = sizeof(buttons)/sizeof(buttons[0]) - 1; i >= 0 ; i--) {

                if(strlen(buttons[i]) > 0) {

			printString("\t\t\t'");
			printString(buttons[i]);
			printString("': function() { $( this ).dialog( ");
			
			if ( action != "" ) {

				//stripQuotes(action,action);
				printString(actions[i]);

			}

			printString(" ); }, \n");

		}

	}

	printString("\t\t}, \n");	

	return 0;

}

int stripQuotes(const char *input, char *output) {

	int len = strlen(input);

	if(len > 0)
		strcpy(output, ++input);
	if(len > 1)
		output[len - 2] = '\0';

	return 0;

}

int resetVariables() {

	buttonIndex = 0;

	positionX = -1;
	positionY = -1;

	color = "";

	title = "";
	label = "";
	button = "";

	action = "";
	imgPath = "";

	for (int i = sizeof(buttons)/sizeof(buttons[0]) - 1; i >= 0 ; i--) {

                if(strlen(buttons[i]) > 0) {

			strcpy(buttons[i], "");

			strcpy(actions[i], "");

		}

	}

	return 0;

}

int printFile(char *path) {

	char character;

	FILE *file;

	file = fopen(path, "r");

	while( ( character = fgetc(file) ) != EOF )
		fputc(character, output);

	fclose(file);

	return 0;

}

int printString(char *string) {

	fputs(string, output);

	return 0;

}

int printPosition() {
	

	if(positionX != -1 && positionY != -1 ) {

		sprintf(position, "[%d,%d], \n", positionX, positionY);
		printString("\t\tposition:");
		printString(position);

	}

	return 0;

}

int printColor() {

	if(color != "") {

		printString("\tbackground:");
		printString(color);
		printString("; \n");

	}

	return 0;

}

int printImage() {


	if(imgPath != "") {

		printString("<img src=");
		printString(imgPath);
		printString(" />");
		printString("<br><br>");

	}

	return 0;

}



int printError(){

	if(title == "") printf("warning: title not defined \n");
	if(label == "") printf("warning: label not defined \n");
	if(buttonIndex == 0) printf("warning: no buttons \n");

	for (int i = 0; i < buttonIndex ; i++) {

		if(actions[i][0] == '\0'){

				printf("warning: button '%s' has no action \n", buttons[i]);
	
		}

	}

	return 0;

}
